#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>


using namespace std;
const int N=5*1e4+10;
const int INF=0x3f3f3f3f;
int n,m;
int f[6];
int dis[6][N];
int vis[N];
int flg[6];
int ret=0x3f3f3f3f;
vector<pair<int,int>> edge[N];
struct cmp{
	bool operator()(pair<int,int>& p1,pair<int,int>& p2){
		if(p1.second==p2.second)
			return p1.first>p2.first;
		return p1.second>p2.second;
	}
};

void dijkstra(int x,int dis[])
{
	memset(vis,0,sizeof(vis));
	dis[x]=0;
	priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> q;
	q.push({x,0});
	while(q.size())
	{
		int r=q.top().first;
		q.pop();
		if(vis[r])
			continue;
		vis[r]=1;
		for(auto& e:edge[r])
		{
			int aim=e.first;
			if(vis[aim])
				continue;
			if(dis[aim]>dis[r]+e.second)
			{
				dis[aim]=dis[r]+e.second;
				q.push({aim,dis[aim]});
			}
		}
	}
	
}
void dfs(int u,int start,int distance)
{
	if(u>5)
	{
		ret=min(ret,distance);
		return;
	}
	for(int i=1;i<6;i++)
	{
		if(!flg[i])
		{
			flg[i]=1;
			
			dfs(u+1,i,distance+dis[start][f[i]]);
			flg[i]=0;
		}
	}
}

int main()
{
	cin>>n>>m;
	f[0]=1;
	for(int i=1;i<6;i++)	
		cin>>f[i];
	memset(dis,0x3f3f3f3f,sizeof(dis));
	for(int i=0;i<m;i++)
	{
		int x,y,t;
		cin>>x>>y>>t;

		edge[x].push_back({y,t});
		edge[y].push_back({x,t});
	}

	for(int i=0;i<6;i++)
	{
		dijkstra(f[i],dis[i]);
	}

	dfs(1,0,0);
	cout<<ret<<endl;
	return 0;
}
